#include <REGX52.H>
#include<intrins.h>
#include"lcd1602.h"

/*
void time(int a)		//@11.0592MHz
{
	while(a--)
	{
		unsigned char i, j;

		_nop_();
		_nop_();
		i = 2;
		j = 15;
		do
		{
			while (--j);
		} while (--i);
	
	}
		
}


char key()
{
	char b = 0;
	char a = 0;
	P1 = 0x0f;
	if(P1_0 == 0)
		a = 4;
	else if(P1_1 == 0)
		a = 3;
	else if(P1_2 == 0)
		a = 2;
	else if(P1_3 == 0)
		a = 1;
	else 
		return 0;
	time(1);
	P1 = 0xf0;
	if(P1_4 == 0)
		a += 12;
	else if(P1_5 == 0)
		a += 8;
	else if(P1_6 == 0)
		a += 4;
	
	while(b < 50 && P1 != 0xf0)
	{
		time(100);
		b++;
	}
	
	if(b >= 50)
		return 0;
	return a;
}    
 
int multiply(int a, int b) {
	int sum = 0;
	while (a) {
		if (a & 1 == 1) { 
			sum = sum + b;
		}
		a = a >> 1;
		b = b << 1;
	}
	
	return sum;
}



int main()
{
	int s1 = 0;
	int s2 = 0;
	char s3 = 0;
	char cal = 0;
	char s2ca = 0;
	char k = 0;
	char yn = 0;
	int sum = 0;
	
	LCD_Init();
	while(1)
	{
		LCD_ShowString(1,1,"calculator");
		LCD_ShowNum(2,1,s1,5);
		if(cal)
		{
			LCD_ShowChar(2,6,cal);
		}
		if(s2ca)
		{
			LCD_ShowNum(2,7,s2,5);
		}
		k = key();
		if(!k)
			continue;
		switch(k)
		{
			case 1:
			case 2:
			case 3:s3 = k;break;
			case 5:
			case 6:
			case 7:s3 = k - 1;break;
			case 9:
			case 10:
			case 11:s3 = k - 2;break;
			case 14:s3 = 0;break;
			case 4:if(!s2ca)cal = '+';break;
			case 8:if(!s2ca)cal = '-';break;
			case 12:if(s2ca)yn = 'y';break;
			case 13:if(!s2ca)cal = '*';break;
			case 15:if(!s2ca)cal = '/';break;
			case 16:yn = 'n';break;
		}
		if(yn == 'n')
		{
			s1 = s2 = s3 = cal = s2ca = yn = 0;
			LCD_Init();
			continue;
		}
		
		if(cal == 0)
		{
			if(s1 > 6553 || (s1 == 6553 && s3 > 5))
			{
				s1 = s3 - (65535 - (s1 * 10));
			}
			else
				s1 = s1 * 10 + s3;
		}
		if(k != 4 && k != 8 && k != 12 && k != 13 && k != 15 && k != 16 && cal != 0)
		{
			s2ca = 1;
			if(s2 > 6553 || (s2 == 6553 && s3 > 5))
			{
				s2 = s3 - (65535 - (s2 * 10));
			}
			else
				s2 = s2 * 10 + s3;
		}
		k = 0;
		if(cal != 0 && yn == 'y')
		{
			if(cal == '+')
			{
				if(65535 - s1 < s2)
					s1 = s2 - (65535 - s1);
				else 
					s1 = s1 + s2;
			}
			if(cal == '-')
				s1 = s1 - s2;
			if(cal == '/')
				s1 = s1/s2;
			if(cal == '*')
			{
				s1 = multiply(s1,s2);
			}
			s2 = 0;
			s2ca = 0;
			yn = 0;
			s3 = 0;
			cal = 0;
			LCD_Init();
		}
	}
	return 0;
}*/
char hours = 0;
char minutes = 0;
char second = 0;
int s = 0;

void init()
{
	
	AUXR |= 0x80;
	//TMOD &= 0xF0;
	//TL0 = 0xCD;
	//TH0 = 0xD4;
	//TF0 = 0;
	
	
	TMOD = TMOD || 0x01;
	TF0 = 0;
	ET0 = 1;
	EA = 1;
	PT0 = 0;
	TH0 = 64535 / 256;
	TL0 = 64535 % 256;
	TR0 = 1;
}

void fun()  interrupt 1
{
	static int s = 0;
	s++;
	if(s > 20)
	{
		init();
		second++;
		if(second == 60)
		{
			minutes ++;
			second = 0;
			if(minutes == 60)
			{
				hours++;
				if(hours == 24)
				hours = 0;
			}
		}
		LCD_Init();
		LCD_ShowString(1,1,"clock:");
		LCD_ShowNum(2,1,hours,2);
		LCD_ShowChar(2,3,':');
		LCD_ShowNum(2,4,minutes,2);
		LCD_ShowChar(2,6,':');
		LCD_ShowNum(2,7,second,2);
		s = 0;
	}
}


int main()
{
	LCD_Init();
	LCD_ShowString(1,1,"clock:");
	LCD_ShowNum(2,1,hours,2);
	LCD_ShowChar(2,3,':');
	LCD_ShowNum(2,4,minutes,2);
	LCD_ShowChar(2,6,':');
	LCD_ShowNum(2,7,second,2);
	init();
	while(1)
	{}
	return 0;
}


